<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            text-align: center;
            width: 100px;
            height: 50px;
            background: pink;
            margin: 50px;
        }

        .box p {
            width: 100px;
            height: 50px;
            line-height: 50px;
        }

        ul {

            width: 100px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            /* background: pink; */
            /* color: white; */
             display: none; 
        }

        .box li {
            background: #ccc;
            /* color: white; */
            list-style-type: none;
            border-bottom: 1px solid black;
        }
        .box li :hover{
            background: pink;
            color: white;
        }
        .box li a{
            display: inline-block;
            width: 100%;
            height: 100%;
            text-decoration: none;
            color: inherit;
        }
    </style>
</head>

<body>
    <!-- .下拉菜单
1.点击菜单的时候显示下拉列表
2.点击列表每一项的时候隐藏下拉列表
3.菜单变成显示的内容 -->
    <div class="box">
        <p>语言</p>
        <ul>
            <li><a href="#">Java</a></li>
            <li><a href="#">Python</a></li>
            <li><a href="#">HTML5</a></li>
            <li><a href="#">PHP</a></li>
            <li><a href="#">C++</a></li>
           
        </ul>
    </div>

    <script>
        //获取目标元素
        var getP = document.querySelector("p");
        var getul = document.querySelector("ul");
        var getli = document.querySelectorAll("li");


        getP.onclick = function () {         // 给p标签绑定点击事件
            getul.style.display = `block`
        }

        getli.forEach(function (item) {       // 遍历li
            // console.log(item)

            item.onclick = function () {      //item就是每一项的li，给item绑定点击事件
                getP.innerHTML = item.innerText;    //把item的文本写进p标签
                getul.style.display = `none`;       //隐藏ul
            }
        })
    </script> 


</body>

</html>